//
//  BeginLayer.cpp
//  identist
//
//  Created by Denveryee on 14-3-12.
//
//

#include "GameScene.h"
#include "BeginLayer.h"
#include "cocos-ext.h"
#include "VisibleRect.h"
#include "ChosePeopleLayer.h"

using namespace extension;

BeginLayer::BeginLayer(){
    musicOn = true;
    soundOn = true;
    height = 1000;

}

BeginLayer::~BeginLayer(){

}

bool BeginLayer::init()
{
    bool pRet = false;
    
    do {
        CC_BREAK_IF(!CCLayer::init());
        
        CCSprite *bg = CCSprite::create("beginLayer/begin_bg_400_640.png");
        bg->setPosition(ccp(400, 640));
        this->addChild(bg);
        
        CCArmatureDataManager::sharedArmatureDataManager()->addArmatureFileInfo("beginLayer/douctor0.png", "beginLayer/douctor0.plist", "beginLayer/douctor.json");
        
        CCArmatureDataManager::sharedArmatureDataManager()->addArmatureFileInfo("beginLayer/douctor1.png", "beginLayer/douctor1.plist", "beginLayer/douctor.json");
    
        doctor0 = cocos2d::extension::CCArmature::create("douctor");
        
        doctor0->getAnimation()->playByIndex(0);
        doctor0->setAnchorPoint(ccp(0.5, 0.5));
        doctor0->setPosition(ccp(VisibleRect::center().x, VisibleRect::center().y-100));
        this->addChild(doctor0);
        
        CCSprite *title = CCSprite::create("beginLayer/begin_title_400_252.png");
        title->setPosition(ccp(400, 1280-252+500));
        this->addChild(title);
        
        title->runAction(CCEaseElasticOut::create(CCMoveBy::create(3, ccp(0, -500))));
        
        CCMenuItem* playBtn = CCMenuItemImage::create("beginLayer/begin_play_n_393_1090.png", "beginLayer/begin_play_p_393_1090.png", this, menu_selector(BeginLayer::menuPlayCallback));
         playBtn->setPosition(ccp(393, 1280-1090));
        
        CCMenuItem* settingBtn = CCMenuItemImage::create("beginLayer/begin_setting_n_735_69.png", "beginLayer/begin_setting_p_735_69.png", this, menu_selector(BeginLayer::menuSettingCallback));
         settingBtn->setPosition(ccp(735, 1280-69));
        
        menuPlay = CCMenu::create(playBtn, settingBtn, NULL);
        menuPlay->setPosition(CCPointZero);
        this->addChild(menuPlay);
        
        CCParticleSystem *system1 = CCParticleSystemQuad::create("beginLayer/oneStar.plist");
        system1->setPosition(ccp(395, 568));
        addChild(system1, 1);
        
        /*******************setting**********************/
        
        settingLayer = CCLayer::create();
        
        CCSprite *settingBg = CCSprite::create("settingLayer/setting_bg_400_614.png");
        settingBg->setPosition(ccp(400, 1280-614+height));
        settingLayer->addChild(settingBg);
        
        musicBtn = CCMenuItemImage::create("settingLayer/setting_music_btn_212_591.png", "settingLayer/setting_music_btn_212_591.png", this, menu_selector(BeginLayer::settingMusicCallback));
        musicBtn->setPosition(ccp(musicOn?212:(212+100), 1280-591+height));
        
        soundBtn = CCMenuItemImage::create("settingLayer/setting_sound_btn_412_534.png", "settingLayer/setting_sound_btn_412_534.png", this, menu_selector(BeginLayer::settingSoundCallback));
        soundBtn->setPosition(ccp(soundOn?466:(466+100), 1280-591+height));
        
        CCMenuItem* closeBtn = CCMenuItemImage::create("settingLayer/setting_close_n_btn_400_738.png", "settingLayer/setting_close_p_btn_401_741.png", this, menu_selector(BeginLayer::settingClose));
        closeBtn->setPosition(ccp(400, 1280-738+height));
        
        CCMenu* menuSetting = CCMenu::create(musicBtn, soundBtn, closeBtn, NULL);
        menuSetting->setPosition(CCPointZero);
        settingLayer->addChild(menuSetting);
        
        settingLayer->setZOrder(2);
        this->addChild(settingLayer);
        
        pRet = true;
        
    } while (0);
    
    return pRet;
}

void BeginLayer::menuPlayCallback(){
    CCScene *scene = CCScene::create();
    scene->addChild(ChosePeopleLayer::create());
    CCDirector::sharedDirector()->replaceScene(CCTransitionFade::create(1, scene));
}

void BeginLayer::menuSettingCallback(){
    menuPlay->setVisible(false);
    
    mohu = CCSprite::create("beginLayer/begin_mohu_bg_400_640.png");
    mohu->setPosition(ccp(400, 640));
    this->addChild(mohu);

    settingLayer->runAction(CCEaseElasticOut::create(CCMoveBy::create(2, ccp(0, -height))));
}

void BeginLayer::settingMusicCallback(){
    if(musicOn){
        CCAction *move = CCMoveBy::create(1, ccp(100, 0));
        musicBtn->runAction(move);
        musicOn = false;
    }else{
        CCAction *move = CCMoveBy::create(1, ccp(-100, 0));
        musicBtn->runAction(move);
        musicOn = true;
    }
}

void BeginLayer::settingSoundCallback(){
    if(soundOn){
        CCAction *move = CCMoveBy::create(1, ccp(100, 0));
        soundBtn->runAction(move);
        soundOn = false;
    }else{
        CCAction *move = CCMoveBy::create(1, ccp(-100, 0));
        soundBtn->runAction(move);
        soundOn = true;
    }

}

void BeginLayer::settingClose(){
    removeChild(mohu, true);
    settingLayer->runAction(CCEaseElasticOut::create(CCMoveBy::create(2, ccp(0, height))));
    menuPlay->setVisible(true);
}